package com.example.druid.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.druid.entity.SysOrder;
import com.example.druid.entity.qo.SysOrderQO;
import com.example.druid.service.SysOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Optional;

/**
 * @Description TODO
 * @date 2021/6/1 11:00
 * @Author liuzhihui
 * @Version 1.0
 */
@RequestMapping("/SysOrder")
@Controller
public class SysOrderController {
    @Autowired
    private SysOrderService orderService;

    /**
     * 分页查询
     * @param sysOrderQO
     * @return
     */
    @GetMapping("/selectByPage")
    @ResponseBody
    public IPage<SysOrder> page(SysOrderQO sysOrderQO) {
        LambdaQueryWrapper<SysOrder> sysOrderDOLambdaQueryWrapper = getSysOrderDOLambdaQueryWrapper(sysOrderQO);
        Page<SysOrder> userPage = new Page<>(1, 10,true);
        IPage<SysOrder> sysOrderIPage = orderService.getBaseMapper().selectPage(userPage, sysOrderDOLambdaQueryWrapper);

        return sysOrderIPage;
    }
    private LambdaQueryWrapper<SysOrder> getSysOrderDOLambdaQueryWrapper(SysOrderQO sysOrderQO) {
        return Wrappers.lambdaQuery(SysOrder.class)
                .eq(ObjectUtil.isNotNull(sysOrderQO.getOrderStatus()), SysOrder::getOrderStatus, sysOrderQO.getOrderStatus())
                .eq(ObjectUtil.isNotNull(sysOrderQO.getPayStatus()), SysOrder::getPayStatus, sysOrderQO.getPayStatus())
                .eq(ObjectUtil.isNotNull(sysOrderQO.getDeliveryStatus()), SysOrder::getDeliveryStatus, sysOrderQO.getDeliveryStatus())
                .eq(ObjectUtil.isNotNull(sysOrderQO.getDeliveryWay()), SysOrder::getDeliveryWay, sysOrderQO.getDeliveryWay())
                .eq(ObjectUtil.isNotEmpty(sysOrderQO.getOrderNo()), SysOrder::getOrderNo, sysOrderQO.getOrderNo())
                .ge(ObjectUtil.isNotNull(sysOrderQO.getOrderTimeStart()), SysOrder::getOrderTime, minimumOfLocalDate(sysOrderQO.getOrderTimeStart()))
                .lt(ObjectUtil.isNotNull(sysOrderQO.getOrderTimeEnd()), SysOrder::getOrderTime, maximumOfLocalDate(sysOrderQO.getOrderTimeEnd()))
                .apply(ObjectUtil.isNotEmpty(sysOrderQO.getHotelOrderStatus()), "detail @@ {0}", "$.orderMessage.orderStatus == \"" + sysOrderQO.getHotelOrderStatus() + "\"")
                .apply(ObjectUtil.isNotNull(sysOrderQO.getCheckinTimeStart()), "detail @@ {0}", "$.orderMessage.checkinTime >= \"" + minimumOfLocalDate(sysOrderQO.getCheckinTimeStart()) + "\"")
                .apply(ObjectUtil.isNotNull(sysOrderQO.getCheckinTimeEnd()), "detail @@ {0}", "$.orderMessage.checkinTime <= \"" + maximumOfLocalDate(sysOrderQO.getCheckinTimeEnd()) + "\"")
                ;
    }
    private String minimumOfLocalDate(LocalDate localDate) {
        return Optional.ofNullable(localDate).map(date -> LocalDateTime.of(date, LocalTime.MIN)).map(date -> DateUtil.format(date, "yyyy-MM-dd HH:mm:ss")).orElse(null);
    }

    private String maximumOfLocalDate(LocalDate localDate) {
        return Optional.ofNullable(localDate).map(date -> LocalDateTime.of(date.plusDays(1), LocalTime.MIN)).map(date -> DateUtil.format(date, "yyyy-MM-dd HH:mm:ss")).orElse(null);
    }
}
